package team.mutong.taxi.ecc;

import java.util.ArrayList;
import java.util.List;

import libsvm.svm_node;
import team.mutong.taxi.ml.*;
import team.mutong.taxi.domain.GPSPoint;
import weka.core.*;

public class EccAlgorithm {

	// testtimes的值根据需要加入的预测点定
	// static int testtimes = 0;
	// static int counts = 5;
	// static svm_node[] testnode = new svm_node[31];
	public static svm_node[] getPoints(List<List<GPSPoint>> sourse,
			GPSPoint point, List<svm_node[]> result, int num,
			svm_node[] testNode) {

		int testtimes = 23;

		// List<List<Double>> listset = new ArrayList<>();
		double[][] dataset = new double[sourse.size()][num * 2];
		boolean flag = true;
		List<svm_node[]> newresult1 = new ArrayList<>();
		List<svm_node[]> newresult2 = new ArrayList<>();
		List<svm_node[]> newresult3 = new ArrayList<>();
		List<svm_node[]> newresult4 = new ArrayList<>();
		List<svm_node[]> newresult5 = new ArrayList<>();
		List<svm_node[]> newresult6 = new ArrayList<>();
		List<svm_node[]> newresult7 = new ArrayList<>();
		List<svm_node[]> newresult8 = new ArrayList<>();

		svm_node nodelist = new svm_node();

		for (int i = 0; i < sourse.size(); i++) {
			// sublist存放每一条路径取出的点
			List<GPSPoint> sublist = new ArrayList<>();
			//System.out.println(sourse.get(i));
			int lenth = sourse.get(i).size();
			int leftnums = 0;
			int position = 0;
			for (int j = 0; j < sourse.get(i).size(); j++) {
				if (sourse.get(i).get(j).getX() == point.getX()
						&& sourse.get(i).get(j).getY() == point.getY()) {
					position = j;
				}
				leftnums = lenth - position - 1;
			}
			sublist.add(sourse.get(i).get(position++));
			for (int k = 0; k < num - 2; k++) {
				sublist.add(sourse.get(i).get(position + leftnums / (num - 2)));
			}
			sublist.add(sourse.get(i).get(lenth - 1));
			for (int n = 0, m = 0; n < num * 2; n = n + 2, m++) {

				dataset[i][n] = sublist.get(m).getX();
				dataset[i][n + 1] = sublist.get(m).getY();
			}

		}
		for (int j = 0; j < num * 2; j++, testtimes++) {
			List<Double> listset = new ArrayList<>();
			for (int i = 0; i < sourse.size(); i++) {
				listset.add(dataset[i][j]);
			}

			if (flag) {
				double temp = svr.svr_cc(result, listset, testNode);
				svm_node node = new svm_node();
				node.index = testtimes + 1;
				node.value = temp;
				testNode[testtimes] = node;

				//System.out.println("temp=" + temp);

				for (int i = 0; i < result.size(); i++) {

					svm_node[] vector = new svm_node[result.get(i).length + 1];

					for (int nums = 0; nums < result.get(i).length; nums++) {
						svm_node new_node = new svm_node();
						new_node.index = nums + 1;
						new_node.value = result.get(i)[nums].value;
						vector[nums] = new_node;
					}
					svm_node addnode = new svm_node();
					addnode.index = result.get(i).length + 1;
					addnode.value = dataset[i][j];
					vector[result.get(i).length] = addnode;
					newresult1.add(i, vector);
				}
				nodelist.index = j;
				nodelist.value = temp;
				flag = false;
			} else {
				switch (j) {
				case 1:
					double temp1 = svr.svr_cc(newresult1, listset, testNode);
					svm_node node1 = new svm_node();
					node1.index = testtimes + 1;
					node1.value = temp1;
					testNode[testtimes] = node1;
					for (int i = 0; i < newresult1.size(); i++) {

						svm_node[] vector = new svm_node[newresult1.get(i).length + 1];

						for (int nums = 0; nums < newresult1.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult1.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult1.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult1.get(i).length] = addnode;
						newresult2.add(i, vector);
					}
					nodelist.index = j;
					nodelist.value = temp1;
					break;
				case 2:
					double temp2 = svr.svr_cc(newresult2, listset, testNode);
					svm_node node2 = new svm_node();
					node2.index = testtimes + 1;
					node2.value = temp2;
					testNode[testtimes] = node2;
					for (int i = 0; i < newresult2.size(); i++) {

						svm_node[] vector = new svm_node[newresult2.get(i).length + 1];

						for (int nums = 0; nums < newresult2.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult2.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult2.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult2.get(i).length] = addnode;
						newresult3.add(i, vector);
					}
					nodelist.index = j;
					nodelist.value = temp2;
					break;
				case 3:
					double temp3 = svr.svr_cc(newresult3, listset, testNode);
					svm_node node3 = new svm_node();
					node3.index = testtimes + 1;
					node3.value = temp3;
					testNode[testtimes] = node3;
					for (int i = 0; i < newresult3.size(); i++) {

						svm_node[] vector = new svm_node[newresult3.get(i).length + 1];

						for (int nums = 0; nums < newresult3.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult3.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult3.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult3.get(i).length] = addnode;
						newresult4.add(i, vector);
					}
					nodelist.index = j;
					nodelist.value = temp3;
					break;
				case 4:
					double temp4 = svr.svr_cc(newresult4, listset, testNode);
					svm_node node4 = new svm_node();
					node4.index = testtimes + 1;
					node4.value = temp4;
					testNode[testtimes] = node4;
					for (int i = 0; i < newresult4.size(); i++) {

						svm_node[] vector = new svm_node[newresult4.get(i).length + 1];

						for (int nums = 0; nums < newresult4.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult4.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult4.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult4.get(i).length] = addnode;
						newresult5.add(i, vector);
					}
					break;
				case 5:
					double temp5 = svr.svr_cc(newresult5, listset, testNode);
					svm_node node5 = new svm_node();
					node5.index = testtimes + 1;
					node5.value = temp5;
					testNode[testtimes] = node5;
					for (int i = 0; i < newresult5.size(); i++) {

						svm_node[] vector = new svm_node[newresult5.get(i).length + 1];

						for (int nums = 0; nums < newresult5.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult5.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult5.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult5.get(i).length] = addnode;
						newresult6.add(i, vector);
					}
					break;
				case 6:
					double temp6 = svr.svr_cc(newresult6, listset, testNode);
					svm_node node6 = new svm_node();
					node6.index = testtimes + 1;
					node6.value = temp6;
					testNode[testtimes] = node6;
					for (int i = 0; i < newresult6.size(); i++) {

						svm_node[] vector = new svm_node[newresult6.get(i).length + 1];

						for (int nums = 0; nums < newresult6.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult6.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult6.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult6.get(i).length] = addnode;
						newresult7.add(i, vector);
					}
					break;
				case 7:
					double temp7 = svr.svr_cc(newresult7, listset, testNode);
					svm_node node7 = new svm_node();
					node7.index = testtimes + 1;
					node7.value = temp7;
					testNode[testtimes] = node7;
					for (int i = 0; i < newresult7.size(); i++) {

						svm_node[] vector = new svm_node[newresult7.get(i).length + 1];

						for (int nums = 0; nums < newresult7.get(i).length; nums++) {
							svm_node new_node = new svm_node();
							new_node.index = nums + 1;
							new_node.value = newresult7.get(i)[nums].value;
							vector[nums] = new_node;
						}
						svm_node addnode = new svm_node();
						addnode.index = newresult7.get(i).length + 1;
						addnode.value = dataset[i][j];
						vector[newresult7.get(i).length] = addnode;
						newresult8.add(i, vector);
					}
					break;
				default:
					break;
				}
			}

		}
//		System.out.println(testNode.length);
//		System.out.println(testNode);
		return testNode;
	}
}
